
console.log('获取到span的dom如下')

chrome.runtime.sendMessage({
  from: 'content',
  subject: 'showPageAction',
})

chrome.runtime.onMessage.addListener((msg, sender, response) => {
  // First, validate the message's structure.
  if ((msg.from === 'popup') && (msg.subject === 'DOMInfo')) {
    // Collect the necessary data. 
    // (For your specific requirements `document.querySelectorAll(...)`
    //  should be equivalent to jquery's `$(...)`.)
    var domInfo = '接收到popup消息，这是返回的'

    // Directly respond to the sender (popup), 
    // through the specified callback.
    response(domInfo)
  }
})

/* toast提示 */
function Toast(text) {
  const m = document.createElement('div')
  const toastId = Date.now() + ''
  m.setAttribute('toastId', toastId)
  m.setAttribute('style', 'background: #000;border-radius: 8px;color: #fff;font-size: 28px;left: 50%;max-width: 62.6%;opacity: 0.7;padding: 0 5px;padding: 0.75rem 1rem;position: fixed;text-align: center;top: 20%;transform: translate(-50%, -50%);z-index: 9999999999;');
  m.className = 'Toast'
  m.id = 'jd-toast'
  m.innerHTML = `<div><p>${text}</p><div>`
  document.body.appendChild(m)
  setTimeout(function () {
    const d = 0.5
    m.style.webkitTransition =
      '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in'
    m.style.opacity = '0'
    setTimeout(function () {
      const node1 = document.getElementById('jd-toast')
      if (node1?.getAttribute('toastId') == toastId)
        document.body.removeChild(node1);
    }, d * 1000)
  }, 2000)
}

var intervalID = null

function compareTime(setting) {
  /* 寻找提交按钮dom */
  let iframeDom = null
  let iframeList = document.getElementsByTagName('iframe')
  for (let i = 0; i < iframeList.length; i++) {
    if (iframeList[i].style.display === '') {
      iframeDom = iframeList[i].contentDocument
      break;
    }
  }

  if (!iframeDom) {
    Toast('未找到[申 报]按钮')
    return
  }

  let btnObjs = iframeDom.getElementsByClassName('el-button')

  let targetBtn = Array.from(btnObjs).filter(item => {
    if (item.lastElementChild.innerHTML === '申 报') {
      return item.lastElementChild
    }
  })

  if (!targetBtn[0]) {
    console.log('未找到[申 报]按钮')
    Toast('未找到[申 报]按钮')
    return
  }

  /* 寻找时钟dom */
  let timeDom = document.getElementsByClassName('header-time-bar')
  if (!timeDom) {
    Toast('未找到[时钟]内容')
    return
  } else {
    let i = 0
    intervalID = setInterval(() => {
      i = ++i
      let t = timeDom[0].firstChild.innerHTML
      let hms = t.split(' ')[1]
      let hour = hms.slice(0, 2)
      let min = hms.slice(3, 5)
      let sec = hms.slice(6, 8)
      console.log('执行第' + i + '次')
      // log.appendChild('<span>执行第' + i + '次</span>')
      chrome.runtime.sendMessage({
        from: 'content',
        index: i,
      })
      if (setting.hour === hour && setting.min === min && setting.sec === sec) {
        targetBtn[0].click()
        clearInterval(intervalID)
        console.log('执行完成')
        // log.appendChild('<span style="color: red">执行完成</span>')
        chrome.runtime.sendMessage({
          from: 'content',
          index: 0,
        })
        Toast('执行')
      }
    }, 10) // 执行频率
  }
}

